<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="input">
    <button class="btn">点击发送消息</button>
    <script>
        let token = localStorage.getItem('id');
        let webSocket = new WebSocket(`ws://127.0.0.1:8080/test?token=${token}`);
        webSocket.onopen = function () {
            console.log("onopen");
        }
        webSocket.onmessage = function () {
            console.log('onmessage');
        }

        webSocket.onerror = function () {
            console.log('onerror');
        }
        webSocket.onclose = function () {
            console.log('onclose');
        }

        let btn = document.querySelector(".btn");
        let text = document.querySelector('#input');
        btn.onclick = function () {
            webSocket.send(text.value);
        }
    </script>
</body>

</html>